/*
 * @Author: Sammer.Sun Sammer.Sun@gaiaworks.cn
 * @Date: 2022-12-20 13:16:12
 * @LastEditors: Sammer.Sun Sammer.Sun@gaiaworks.cn
 * @LastEditTime: 2023-05-19 13:30:18
 * @FilePath: /recruit-admin-ui/src/directive/common/resizeHeight.js
 */
import { debounce } from 'lodash';
let ele = null;
let tableHeight = null;
let maxHeight = null; // 设置了最大值的 不再触发高度自适应
let otherHeight = 0;
export default {
  updated(el, value) {},
  mounted(el, { value, arg }) {
    console.log(value);
    tableHeight = value.tableHeight;
    otherHeight = value.otherHeight || 0;
    maxHeight = value.tableHeight.maxHeight || null; // 设置了最大值的 不再触发高度自适应
    ele = el;
    ele.style.overflow = 'auto';
    requestIdleCallback(() => {
      if (!maxHeight) {
        getTableHeight();
      }
      {
        tableHeight.height = maxHeight + 'px';
      }
    });
    if (!maxHeight) {
      window.addEventListener('resize', getTableHeight);
    }
  },
  unmounted(el) {
    ele = null;
    if (!maxHeight) {
      window.removeEventListener('resize', getTableHeight);
    }
  }
};
const getTableHeight = debounce(function () {
  if (ele) {
    const { top } = ele.getBoundingClientRect();
    //    document.querySelector('.pagination-container').style.padding='16px 0'
    const paginationHeight = document.querySelector('.pagination-container')
      ? 65
      : 0; // 粗暴实现，有分页就计算分也的高度
    tableHeight.height =
      'calc(100vh - ' + (top + paginationHeight + otherHeight) + 'px)';
  }
}, 200);
